<?php

namespace app\index\controller;

use think\Db;
use Qcloud\Sms\SmsSingleSender;

class Home extends Base
{
    public function getServiceList()
    {

        $userType = 0;
        $res = 0;
        input('userType') ? $userType = input('userType') : $this->err('缺少用户类型！');
        input('cid') ? $cid = input('cid') : $this->err('缺少用户主键！');
        input('city') ? $city = input('city') : true;
        $user_res = model('user')->where(['id' => $cid])->find();

        $field = 'u.openid,u.id as cid,u.userType,u.province,u.city,u.district,u.avatar,u.street,u.name,u.job as jobId,u.age,u.workCity,DATE_FORMAT(beginTime, \'%H:%i\') as beginTime,
        DATE_FORMAT(endTime, \'%H:%i\') as endTime,u.workYear,u.workDuty,u.maxSalary,u.minSalary,u.rest,j.only,
        u.legalRest,u.introduction,u.pictureSrc1,u.pictureSrc2,u.pictureSrc3,u.videoSrc,u.videoPicSrc,u.appearance,u.isOpenFollow,u.isOpenChat,u.isHiddenMobile,j.name as jobName,
        ur2.isFollow,j.jobSalary,u.isGoodWorker,u.isGoodEmployer';

        $a_field = 'u.openid,u.id as cid,u.userType,u.name,u.avatar,j.only,u.introduction,u.pictureSrc1,u.pictureSrc2,u.pictureSrc3,u.videoSrc,u.videoPicSrc,u.appearance,
        u.isOpenFollow,u.isOpenChat,u.isHiddenMobile,j.name as jobName,ue.ageStart,ue.ageEnd,ue.jobId,ue.province,ue.city,ue.district,ue.street,ue.workCity,ue.beginTime,ue.endTime,ue.workyearStart,ue.workyearEnd,
        ue.workDuty,ue.appearance,ue.maxSalary,ue.minSalary,ue.rest,ue.legalRest
,ur2.isFollow,j.jobSalary,u.isGoodWorker,u.isGoodEmployer';

            $where = [
                'u.userStatus' => 1,
                'u.userType' => 2,
                'u.isPushExpect' => 1,
                'u.city' => $user_res['city'],
            ];
            $a_where = [
                'u.userStatus' => 1,
                'u.userType' => 1,
                'u.isPushExpect' => 1,
                'ue.city' => $user_res['city'],
            ];

            if (input('city')) {
                $where['u.city'] = input('city');
                $a_where['ue.city'] = input('city');
            }
          /*  if($user_res['isPushExpect']==0){
                $this->err('请前往雇一个页面发布信息！');
            }*/
            switch ($userType) {
                // 雇主端
                case 1:
                    //雇主查阿姨
                    $res = model('user')
                        ->alias('u')
                        ->field($field)
                        ->join('job j ', 'j.id = u.job', 'left')
                        ->join('user_relation ur2', 'u.id = ur2.workId and ' . $cid . ' = ur2.employeeId', 'left')
                        ->where($where)
                        ->group('u.id')
                        ->order('u.id desc')
                        ->paginate(10)
                        ->toArray();
                    break;
                // 阿姨端
                case 2:

                    // 阿姨查雇主
                    $res = model('user_expect')
                        ->alias('ue')
                        ->field($a_field)
                        ->join('job j', 'j.id=ue.jobId', 'left')
                        ->join('user u', 'u.id = ue.cid', 'left')
                        ->join('user_relation ur2', 'ue.cid = ur2.employeeId and ' . $cid . ' = ur2.workId', 'left')
                        ->where($a_where)
                        ->order('ue.id desc')
                        ->paginate(10)
                        ->toArray();
                    break;
                default:
                    $this->err('用户类型错误！');
                    break;
            }

            //echo(model('user')->getLastSql());exit;
            $res ? $this->suc($res) : $this->err('获取信息失败');

        }
    /**
     * 验证验证码
     */

    public function checkCaptcha()
    {
        $mobile = "";
        $captcha = "";
        input('mobile') ? $mobile = input('mobile') : $this->err('缺少手机号！');
        input('captcha') ? $captcha = input('captcha') : $this->err('缺少验证码！');
        cache("m" . $mobile) == $captcha ? $this->suc('1') : $this->err('验证失败！');
    }

    /**
     * 发送验证码
     */
    public function sendCaptcha()
    {
        $appid = config('qcloudsms.AppId');
        $appkey = config('qcloudsms.AppKey');
        $templId = 163483; //短信模板id  163483-验证码模板
        $mobile = "";

        input('mobile') ? $mobile = input('mobile') : $this->err('缺少手机号！');

        checkMobile($mobile) ? true : $this->err('手机号格式不正确！');

        $captcha = rand(1000, 9999);
        cache('m' . $mobile, $captcha,60 * 10);
        try {
            $sender = new SmsSingleSender($appid, $appkey);
            $params = [$captcha, "10"];
            // 假设模板内容为：测试短信，{1}，{2}
            $result = $sender->sendWithParam("86", $mobile, $templId,
                $params, "", "", "");
            $rsp = json_decode($result);
            if($rsp->result == 0){
                $this->suc('发送成功！');
            } else{
                $this->err('发送失败！');
            }
        } catch(\Exception $e) {
            $this->err($e);
        }
    }


    public function getSystemConfig(){
        $where = [];

        // 查找启用配置项
        $where['status'] = 1;

        $res = model('system_config')->where($where)->find();
        $res ? $this->suc($res) : $this->err('获取系统配置详情失败');
    }

    public function test(){
        $url = 'https://recognition.image.myqcloud.com/face/idcardcompare';
        $body = [
            'appid' => 1257144743,
            'idcard_number' => 1257144743,
            'appid' => 1257144743,
            'appid' => 1257144743,
        ];
        $res = curlPost();
    }

}
